import Vue from 'vue'
import Vuex from 'vuex'
import { request } from './utils/wx'
import { apiDomain } from './utils/constant'

Vue.use(Vuex)

const API = {
  activityCreate: `${apiDomain}/api/activity/create`,
  activityList: `${apiDomain}/api/activity/list`,
  activityDetail: `${apiDomain}/api/activity`,
  /** 获取参与活动的玩家，GET */
  activityPlayer: `${apiDomain}/api/activity/player`,
  qaList: `${apiDomain}/api/qa/list`,
  activityCount: `${apiDomain}/api/player/activitycount`,
  playerUpsert: `${apiDomain}/api/player/upsert`,
  /** 获取玩家参与的活动列表，包括参与的抽奖、中奖记录，GET */
  playerActivity: `${apiDomain}/api/player/activity`,
  /** 参与抽奖，POST */
  playerJoinActivity: `${apiDomain}/api/player/joinActivity`,
  /** 设置中奖活动地址，POST */
  playerSetActivityAddr: `${apiDomain}/api/player/setActivityAddr`,
  /** 获取openId，GET */
  wechatAuth: `${apiDomain}/api/wechat`,
  /** 提交赞助商信息，POST */
  createSponsor: `${apiDomain}/api/message/create/sponsor`,
  /** 获取赞助商列表，GET */
  sponsorList: `${apiDomain}/api/sponsor/list`,
  /** 获取玩家的金币日志，GET */
  coinLog: `${apiDomain}/api/player/goldlog`,
  /** 创建金币订单，POST */
  createCoinOrder: `${apiDomain}/api/player/createOrder`,
  /** 获取订单列表，POST */
  coinOrderList: `${apiDomain}/api/order/list`,
  /** 获取订单详情，GET */
  orderDetail: `${apiDomain}/api/order`,
  /** 获取用户统计数据，GET */
  playerStatistic: `${apiDomain}/api/player/statistic`,
  /** 获取配置数据，GET */
  config: `${apiDomain}/api/config`,
  /** 获取图形验证码，GET */
  imageCode: `${apiDomain}/api/code`,
  /** 图形验证码校验，POST */
  submitImageCode: `${apiDomain}/api/player/bindPhoneCode`,
  /** 发送短信验证码 */
  sendSms: `${apiDomain}/api/player/sendBindPhone`,
  /** 绑定手机号，POST */
  bindPhone: `${apiDomain}/api/player/bindPhone`,
  /** 兑换金币 */
  activeCoinOrder: `${apiDomain}/api/order/activate`,
  /** 获取玩家信息 */
  playerInfo: `${apiDomain}/api/player`,
  /** 获取消息列表 */
  newsList: `${apiDomain}/api/news`,
  /** 我的下级 */
  playerChildren: `${apiDomain}/api/player/children`
}

const store = new Vuex.Store({
  state: {},
  mutations: {},
  actions: {
    // 创建活动
    async createActivity ({ state }, data) {
      const res = await request({
        url: API.activityCreate,
        method: 'POST',
        data
      })
      return res
    },
    // 获取活动列表
    async getActivityList ({ state }, data) {
      const res = await request({
        url: API.activityList,
        method: 'GET',
        data
      })
      return res.result
    },
    // 获取活动详情
    async getActivityDetail ({ state }, data) {
      const res = await request({
        url: API.activityDetail,
        data
      })
      return res.result
    },
    // 获取参与活动的玩家
    async getActivityPlayer ({ state }, data) {
      const res = await request({
        url: API.activityPlayer,
        data
      })
      return res.result
    },
    // 获取常见问题列表
    async getQaList () {
      const res = await request({
        url: API.qaList,
      })
      return res.result
    },
    // 获取玩家参与活动的统计信息
    async getActivityCount ({ state }, data) {
      const res = await request({
        url: API.activityCount,
        data
      })
      return res.result
    },
    // 玩家创建／更新信息
    async doPlayerUpsert ({ state }, data) {
      const res = await request({
        url: API.playerUpsert,
        method: 'POST',
        data
      })
      return res.result
    },
    // 获取玩家参与的活动列表，包括参与的抽奖、中奖记录
    async getPlayerActivity ({ state }, data) {
      const res = await request({
        url: API.playerActivity,
        data
      })
      return res.result
    },
    // 参与抽奖
    async joinActivity ({ state }, data) {
      const res = await request({
        url: API.playerJoinActivity,
        method: 'POST',
        data,
        toastText: '参与中...'
      })
      return res
    },
    // 获取openid
    async wechatAuth ({ state }, code) {
      const res = await request({
        url: API.wechatAuth,
        data: { code }
      })
      return res.result
    },
    // 提交赞助商信息
    async createSponsor ({ state }, data) {
      const res = await request({
        url: API.createSponsor,
        method: 'POST',
        data
      })
      return res.result
    },
    // 获取赞助商列表
    async getSponsorList ({ state }, data) {
      const res = await request({
        url: API.sponsorList,
        data
      })
      return res.result
    },
    // 设置中奖活动地址
    async setActivityAddr ({ state }, data) {
      const res = await request({
        url: API.playerSetActivityAddr,
        method: 'POST',
        data
      })
      return res.result
    },
    // 获取玩家的金币日志
    async queryCoinLog ({ state }, data) {
      const res = await request({
        url: API.coinLog,
        data
      })
      return res.result
    },
    // 创建金币订单
    async createCoinOrder ({ state }, data) {
      const res = await request({
        url: API.createCoinOrder,
        method: 'POST',
        data
      })
      return res
    },
    // 获取交易市场订单列表
    async queryCoinOrderList ({ state }, data) {
      const res = await request({
        url: API.coinOrderList,
        data
      })
      return res.result
    },
    // 获取交易市场订单详情
    async queryOrderDetail ({ state }, data) {
      const res = await request({
        url: API.orderDetail,
        data
      })
      return res.result
    },
    // 获取用户统计数据
    async queryPlayerStatistic ({ state }, data) {
      const res = await request({
        url: API.playerStatistic,
        data
      })
      return res.result
    },
    // 获取配置数据
    async queryConfig ({ state }, data) {
      const res = await request({
        url: API.config,
        data
      })
      return res.result
    },
    // 获取图形验证码
    async getImageCode ({ state }, data) {
      const res = await request({
        url: API.imageCode,
        data
      })
      return res.result
    },
    // 校验图形验证码
    async submitImageCode ({ state }, data) {
      const res = await request({
        url: API.submitImageCode,
        method: 'POST',
        data
      })
      return res.result
    },
    // 发送短信验证码
    async sendSms ({ state }, data) {
      const res = await request({
        url: API.sendSms,
        method: 'POST',
        data
      })
      return res.result
    },
    // 绑定手机号
    async bindPhone ({ state }, data) {
      const res = await request({
        url: API.bindPhone,
        method: 'POST',
        data
      })
      return res.result
    },
    // 兑换金币
    async activeCoinOrder ({ state }, data) {
      const res = await request({
        url: API.activeCoinOrder,
        method: 'POST',
        data
      })
      return res
    },
    // 获取玩家信息
    async getPlayerInfo ({ state }, data) {
      const res = await request({
        url: API.playerInfo,
        data
      })
      return res.result
    },
    // 获取消息列表
    async getNewsList ({ state }, data) {
      const res = await request({
        url: API.newsList,
        data,
        noToast: true
      })
      return res.result
    },
    // 获取我的下级列表
    async getPlayerChildren ({ state }, data) {
      const res = await request({
        url: API.playerChildren,
        data
      })
      return res.result
    }
  }
})

export default store
